package 笔试;

/**
 * @Date 2024/10/26 9:43
 * @description: 中国电信笔试
 * .
 * @Author LittleNight
 */
public class zhongguodianxin {

    public static long kinterval (int[] a, int x, int k) {
        // write code here
        // 给定一个整数序列a以及两个整数x,k, 求有多少个区间[L,R] 使得该区间中恰好有k个数满足能被x整除
        int n = a.length;
        long[] pre = new long[n + 1];
        for(int i = 1; i <= n; i++) {
            pre[i] = pre[i - 1] + (a[i - 1] % x == 0 ? 1 : 0);
        }
        long res = 0;
        for(int i = 1; i <= n; i++) {
            for(int j = i; j <= n; j++) {
                int cnt = (int) (pre[j] - pre[i - 1]);
                if(cnt == k) {
                    res++;
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        // seven_good_number(7);
        int[] arr = new int[]{1,2,3,4};
        long count = kinterval(arr, 2, 1);
        System.out.println(count);
    }

    public static int seven_good_number (int n) {
        // write code here
        int ret = 0;
        for(int i = 6; i <= n; i++) {
            if(isGood(n)) {
                ret++;
            }
        }
        System.out.print(ret);
        return ret;
    }

    public static boolean isGood(int num) {
        char[] ch = String.valueOf(num).toCharArray();
        int n = ch.length;
        if(ch[0] == '7' && ch[n - 1] == '7') return true;
        return false;
    }

    // 判断一个数是不是7好数: 7好数就是当且仅当开头和结尾都是7就是7好数
    public static boolean isGood2(int n){
        String s = String.valueOf(n);
        return s.charAt(0) == '7' && s.charAt(s.length() - 1) == '7';
    }
}
